package com.csw.controller;

import com.csw.entity.Dir;
import com.csw.entity.File;
import com.csw.entity.User;
import com.csw.service.DirService;
import com.csw.service.FileService;
import com.csw.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("hide")
public class HideController {
    @Autowired
    private DirService dirService;
    @Autowired
    private FileService fileService;

    @RequestMapping("queryAll")
    public String queryAll(HttpSession session, String hidePwd) {

        User user = (User) session.getAttribute("user");
            if (user == null) {
                return "redirect:/user/logOut";
            }
            if (user.getHidePwd() == null) {
                session.setAttribute("message", "你的隐藏密码为空，请去我的个人中心设置隐藏密码");
                return "redirect:/jsp/front/main/detail/hide.jsp";
            } else if (hidePwd == null) {
                session.setAttribute("message", "对不起，你输入的密码为空");
                return "redirect:/jsp/front/main/detail/hide.jsp";
            }
            String pwd = MD5Utils.getPassword(hidePwd) + user.getHideSalt();
            if (!pwd.equals(user.getHidePwd())) {
                session.setAttribute("message", "你输入的密码不正确，请重新输入,或者去我的个人中心重新设置密码");
                return "redirect:/jsp/front/main/detail/hide.jsp";
            } else {

                int status = 2;
                List<Dir> dirs = dirService.selectByStatus(status, user);
                List<File> files = fileService.selectByStatus(status, user);
                session.setAttribute("dirs", dirs);
                session.setAttribute("files", files);
                return "redirect:/jsp/front/main/detail/hide.jsp";
            }

    }


    @RequestMapping("getMessage")
    public String getMessage(HttpSession session) {

        String message = (String) session.getAttribute("message");
            return message;

    }

    /**
     * @param session
     * @return
     */
    @RequestMapping("cleMessage")
    public String cleMessage(HttpSession session) {

        session.setAttribute("message", null);
            return "redirect:/jsp/front/loginHide.jsp";

    }

    /**
     * @param fileId
     * @param session
     * @return
     */
    @RequestMapping("/yrdel")
    public String yrdel(String fileId, HttpSession session) {

        File file = fileService.selectFileById(fileId);
            file.setStatus(2);
            fileService.updateStatus(file);
            session.setAttribute("message", "文件移入隐藏文件夹成功");
            return "redirect:/mydoc/queryOne";

    }

    /**
     * @param pid
     * @param session
     * @return
     */
    @RequestMapping("/yrdelDir")
    public String yrdelDir(String pid, HttpSession session) {

        Dir dir1 = dirService.selectById(pid);
            dir1.setStatus(2);
            dirService.updateStatue(dir1);
            session.setAttribute("message", "文件夹移入隐藏文件夹成功");
            return "redirect:/mydoc/queryOne";

    }

}
